<?php
if(defined("_PARA_FORUM_MESSAGE"))
    return;
define("_PARA_FORUM_MESSAGE", 1);

require_once("ForumMessageBody.php");
require_once("HTMLable.php");

class ForumMessage extends HTMLable {

    public $id;
    public $parent_id;
    public $thread_id;
    public $screen_name;
    public $uid;
    public $subject;
    public $date;
    public $tags;
    public $xtras;

    public  $children  = null;
    private $body      = null;


    public function ForumMessage( $id,
                                  $parent_id,
                                  $thread_id,
                                  $screen_name,
                                  $uid,
                                  $subject,
                                  $date,
                                  $tags,
                                  $xtras
        ) {

        $this->id          = $id;
        $this->parent_id   = $parent_id;
        $this->thread_id   = $thread_id;
        $this->screen_name = $screen_name;
        $this->uid         = $uid;
        $this->subject     = $subject;
        $this->date        = $date;
        $this->tags        = $tags;
        $this->xtras       = $xtras;

        $this->children = null;
    }


    public function add_child ( $message ) {
        if($this->id == $message->parent_id) {
            if($this->children == null) {
                $this->children = array( $message );
            }
            else {
                array_push($this->children, $message);
            }
            return true;
        }

        if($this->children == null)
            return false;

        foreach ($this->children as $child) {
            if($child->add_child( $message))
                return true;
        }

        return false;
    }


    public function set_body( $body ) {
        $body->set_parent( $this );
        $this->body = $body;
    }


    public function get_body( $body ) {
        return $this->body;
    }


    // find message in tree --------------
    public function find_message( $id ) {
        if( $id == $this->id )
            return $this;

        if( $this->children )
            foreach( $this->children as $child )
                if($found_child = $child->find_message( $id ) )
                    return $found_child;

        return null;
    }


    // Find all ids in the tree
    public function get_tree_ids() {
        $aa = array();
        return $this->_get_tree_ids( $aa );
    }


    private function _get_tree_ids( &$array ) {

        array_push( $array, $this->id );

        if( $this->children )
            foreach( $this->children as $child )
                $child->_get_tree_ids( $array );

        return $array;
    }


    public function count() {
        if($this->children == null)
            return 1;
        else {
            $count = 1;
            foreach( $this->children as $child)
                $count += $child->count();
            return $count;
        }
    }


    public function htmlize( $options ) {

        if( $options['thread_head'] ) {
            echo "<div class=\"para_forum_msg\" id=\"para_forum_msg_$this->id\">\n";
            $on_click = "onclick=\"para_forum_uev_view_thread($this->thread_id)\"";
            echo "<div class=\"para_forum_msg_subject_bigger\" $on_click>",
                htmlspecialchars( $this->subject ),
            "</div>\n";
            echo "<div class=\"para_forum_msg_screen_name\">",
                htmlspecialchars( $this->screen_name ),
                "</div>\n";
            echo "<div class=\"para_forum_msg_date\">",
                Helpers::nice_timestamp( $this->date ),
                "</div>\n";
        }

        else {
            echo "<div class=\"para_forum_msg\" id=\"para_forum_msg_$this->id\">\n";
  
            if( $options['preview'])
                $on_click = '';
            else
                $on_click = "onclick=\"para_forum_uev_toggle_body($this->id)\"";

            echo "<div class=\"para_forum_msg_subject\" $on_click>",
                htmlspecialchars( $this->subject ),
            "</div>\n";
            echo "<div class=\"para_forum_msg_screen_name\">",
                htmlspecialchars( $this->screen_name ),
                "</div>\n";
            echo "<div class=\"para_forum_msg_date\">",
                htmlspecialchars( $this->date ),
                "</div>\n";

            if( $options['preview'] && $this->body ) {
                echo "<div id=\"para_forum_msgbody_container_$this->id\" class=\"para_shown\">";
                $this->body->htmlize( array('no_controls' => 1) );
                echo "</div>\n";
            }

        }


        if( $options['expanded'] ) {

            if( $options['bodies'] && in_array($this->id, $options['bodies'] ) ) {
                echo "<div id=\"para_forum_msgbody_container_$this->id\" class=\"para_shown\">";
                $this->body->htmlize( array() );
                echo "</div>\n";
            }
            else {
                echo "<div id=\"para_forum_msgbody_container_$this->id\" class=\"para_hidden\"></div>\n";
            }

            if( $this->children != null) {
                echo "<div class=\"para_forum_msg_children\">";
                foreach( $this->children as $child ) {
                    $child->htmlize( $options );
                }
                echo "</div>\n";
            }
        }

        echo "</div>\n";

    }


    function adm_htmlize(  $options ) {
        echo '<div class="padm_forum_msg">';
        if( $options['thread_head'] )
            echo '<input type="checkbox" name="pf_thread[]" class="checkbox" value="', $this->thread_id, '" />';
        else
            echo '<input type="checkbox" name="pf_msg[]" class="checkbox" value="', $this->thread_id, ':', $this->id, '" />';

        echo '<span class="padm_forum_msg_subject">',     htmlspecialchars( $this->subject ), "</span>";
        echo '<span class="padm_forum_msg_screen_name">', htmlspecialchars( $this->screen_name ), "</span>";
        echo '<span class="padm_forum_msg_date">',        htmlspecialchars( $this->date ), "</span>";

        $options['thread_head'] = false;

        if( $this->children != null) {
            echo "<div class=\"padm_forum_msg_children\">";
            foreach( $this->children as $child ) {
                $child->adm_htmlize( $options );
            }
            echo "</div>\n";
        }

        echo "</div>\n";
    }

}

?>